package com.ydd.app.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Repository;

import com.ydd.app.bean.common.CustomerBean;
import com.ydd.app.bean.common.EmployeesBean;
import com.ydd.app.bean.common.OrganizeBean;
import com.ydd.app.bean.common.OrganizeOperatorConfigBean;
import com.ydd.app.bean.input.CreteUserRequest;
import com.ydd.app.bean.input.CustomerAccountRequest;
import com.ydd.app.dao.AccountDao;
import com.ydd.common.hibernate3.JdbcBaseDao;
import com.ydd.common.util.CommonUtil;

@Repository
public class AccountDaoImpl  extends JdbcBaseDao  implements AccountDao{

	public String SELECT_LAST_INSERT_ID() {
		String a = null;
		SqlRowSet set = getJdbcTemplate().queryForRowSet("SELECT IDENT_CURRENT('TBL_UserInfo') as id");
		while(set.next()){
			System.out.println(set.getString("id"));
			a = set.getString("id");
		}
		System.out.println(a);
		return a;
	}
	
	@Override
	public CustomerBean queryCustomerByAccount(final String account) {
		SqlRowSet set = getJdbcTemplate().queryForRowSet(AccountDaoSql.QUERY_CUSTOMER_BY_CUSTOMER_ID_SQL,account);
		while(set.next()){
			final CustomerBean customer = new CustomerBean();
			customer.setCanYeDingWei(set.getString("CanYeDingWei"));
			customer.setChengBen(set.getString("ChengBen"));
			customer.setDianJia(set.getString("DianJia"));
			customer.setDiJia(set.getString("DiJia"));
			customer.setDiYu(set.getString("DiYu"));
			customer.setDuiJieBuMen(set.getString("DuiJieBuMen"));
			customer.setDuiJieBuMenFuZhiRen(set.getString("DuiJieBuMenFuZhiRen"));
			customer.setDuiJieBuMenFuZhiRenShouJi(set.getString("DuiJieBuMenFuZhiRenShouJi"));
			customer.setDuiJieBuMenFuZhiRenYouXiang(set.getString("DuiJieBuMenFuZhiRenYouXiang"));
			customer.setFuWuHuanJin(set.getString("FuWuHuanJin"));
			customer.setFZR(set.getString("FZR"));
			customer.setGuiHuaYongDiMianJi(set.getString("GuiHuaYongDiMianJi"));
			customer.setGuLiYinJinCanYe(set.getString("GuLiYinJinCanYe"));
			customer.setJiaoTongZhuangKuang(set.getString("JiaoTongZhuangKuang"));
			customer.setLaiYuan(set.getString("LaiYuan"));
			customer.setName(set.getString("Name"));
			customer.setNaoDongLiShuLiang(set.getString("NaoDongLiShuLiang"));
			customer.setPeiTaoQingKuang(set.getString("PeiTaoQingKuang"));
			customer.setShangHui(set.getString("ShangHui"));
			customer.setShengShiXian(set.getString("ShengShiXian"));
			customer.setShouJi(set.getString("ShouJi"));
			customer.setSuiJia(set.getString("SuiJia"));
			customer.setSuiShouZhengChe(set.getString("SuiShouZhengChe"));
			customer.setSysID(set.getString("SysID"));
			customer.setSystemOper(set.getString("SystemOper"));
			customer.setSystemTime(set.getString("SystemTime"));
			customer.setUserID(set.getString("UserID"));
			customer.setUserPAssword(set.getString("UserPAssword"));
			customer.setXianYouZhuYaoCanYe(set.getString("XianYouZhuYaoCanYe"));
			customer.setYouBian(set.getString("YouBian"));
			customer.setZhiMingQiYe(set.getString("ZhiMingQiYe"));
			return customer;
		}
		return null;
	}
	
	@Override
	public CustomerBean queryCustomerById(final String userId) {
		SqlRowSet set = getJdbcTemplate().queryForRowSet(AccountDaoSql.QUERY_CUSTOMER_BY_ID_SQL,userId);
		while(set.next()){
			final CustomerBean customer = new CustomerBean();
			customer.setCanYeDingWei(set.getString("CanYeDingWei"));
			customer.setChengBen(set.getString("ChengBen"));
			customer.setDianJia(set.getString("DianJia"));
			customer.setDiJia(set.getString("DiJia"));
			customer.setDiYu(set.getString("DiYu"));
			customer.setDuiJieBuMen(set.getString("DuiJieBuMen"));
			customer.setDuiJieBuMenFuZhiRen(set.getString("DuiJieBuMenFuZhiRen"));
			customer.setDuiJieBuMenFuZhiRenShouJi(set.getString("DuiJieBuMenFuZhiRenShouJi"));
			customer.setDuiJieBuMenFuZhiRenYouXiang(set.getString("DuiJieBuMenFuZhiRenYouXiang"));
			customer.setFuWuHuanJin(set.getString("FuWuHuanJin"));
			customer.setFZR(set.getString("FZR"));
			customer.setGuiHuaYongDiMianJi(set.getString("GuiHuaYongDiMianJi"));
			customer.setGuLiYinJinCanYe(set.getString("GuLiYinJinCanYe"));
			customer.setJiaoTongZhuangKuang(set.getString("JiaoTongZhuangKuang"));
			customer.setLaiYuan(set.getString("LaiYuan"));
			customer.setName(set.getString("Name"));
			customer.setNaoDongLiShuLiang(set.getString("NaoDongLiShuLiang"));
			customer.setPeiTaoQingKuang(set.getString("PeiTaoQingKuang"));
			customer.setShangHui(set.getString("ShangHui"));
			customer.setShengShiXian(set.getString("ShengShiXian"));
			customer.setShouJi(set.getString("ShouJi"));
			customer.setSuiJia(set.getString("SuiJia"));
			customer.setSuiShouZhengChe(set.getString("SuiShouZhengChe"));
			customer.setSysID(set.getString("SysID"));
			customer.setSystemOper(set.getString("SystemOper"));
			customer.setSystemTime(set.getString("SystemTime"));
			customer.setUserID(set.getString("UserID"));
			customer.setUserPAssword(set.getString("UserPAssword"));
			customer.setXianYouZhuYaoCanYe(set.getString("XianYouZhuYaoCanYe"));
			customer.setYouBian(set.getString("YouBian"));
			customer.setZhiMingQiYe(set.getString("ZhiMingQiYe"));
			return customer;
		}
		return null;
	}

	@Override
	public EmployeesBean queryEmployeesByAccount(final String account) {
		SqlRowSet set = getJdbcTemplate().queryForRowSet(AccountDaoSql.QUERY_EMPLOYEES_BY_ID_SQL,account);
		while(set.next()){
			final EmployeesBean employeesBean = new EmployeesBean();
			final OrganizeOperatorConfigBean organC = new OrganizeOperatorConfigBean();
			final OrganizeBean org = new OrganizeBean();
			org.setCreateDate(set.getString("Create_Date"));
			org.setExplain(set.getString("Explain"));
			org.setLevel(set.getString("Level"));
//			org.setOrganizeId(set.getString(""));
			org.setOrganizeName(set.getString("OrganizeName"));
			org.setParentID(set.getString("Parent_ID"));
			org.setSzm(set.getString("SZM"));
			org.setTitle(set.getString("Title"));
			org.setTitlePicture(set.getString("TitlePicture"));
			org.setUnitID(set.getString("UNIT_ID"));
			org.setUrl(set.getString("URL"));
			
			organC.setMemo(set.getString("Memo"));
			organC.setOperatorTypeId(set.getString("OperatorType"));
//			organC.setOperatorTypeName(set.getString(""));
			organC.setOrganizeId(set.getString("Organizeid"));
			
			employeesBean.setActiveTime(set.getString("Active_Time"));
			employeesBean.setAddress(set.getString("Address"));
			employeesBean.setBirthDate(set.getString("Birthday"));
			employeesBean.setEmployeesEmial(set.getString("Email"));
			employeesBean.setEmployeesId(set.getString("ID"));
			employeesBean.setEmployeesMobile(set.getString("Phone"));
			employeesBean.setEmployeesName(set.getString("OperatorName"));
			employeesBean.setUnitID(set.getString("UNIT_ID"));
			employeesBean.setEmployeesPassowrd(set.getString("Password"));
			employeesBean.setEmployeesState(set.getString("State"));
			employeesBean.setOrganize(org);
			employeesBean.setOrganizeOperatorConfigBean(organC);
			employeesBean.setPrivateEmployeesPassowrd(set.getString("Password"));
			employeesBean.setRemark(set.getString("Remark"));
			employeesBean.setRole(set.getString("Role"));
			employeesBean.setSex(set.getString("Gender"));
			employeesBean.setTnvalidTime(set.getString("Invalid_Time"));
			employeesBean.setConfig("1".equals(set.getString("IsConfig")));
			
			return employeesBean;
		}
		return null;
	}

	@Override
	public String queryEmployeesToken(final String account) {
		final List<String> tokens = getJdbcTemplate().queryForList(AccountDaoSql.QUERY_EMPLOYEES_TOKEN_SQL,String.class,account);
		if( null != tokens && tokens.size() > 0 ) {
			return tokens.get(0);
		}
		return null;
	}

	@Override
	public String queryCustomerToken(final String account) {
		final List<String> tokens = getJdbcTemplate().queryForList(AccountDaoSql.QUERY_CUSTOMER_TOKEN_SQL,String.class,account);
		if( null != tokens && tokens.size() > 0 ) {
			return tokens.get(0);
		}
		return null;
	}

	@Override
	public boolean updateTokenForEmployees(final String account,final  String token) {
		final int row = getJdbcTemplate().update(AccountDaoSql.UPDATE_INTO_TOKEN_FOR_EMPLOYESS_SQL,token,account); 
		if( row ==  0 ) {
			return getJdbcTemplate().update(AccountDaoSql.INSERT_INTO_TOKEN_FOR_EMPLOYESS_SQL,token,account) > 0 ;
		}
		return true;
	}

	@Override
	public boolean updateTokenForCustomer(final String account,final  String token) {
		final int row = getJdbcTemplate().update(AccountDaoSql.UPDATE_INTO_TOKEN_FOR_CUSTOMER_SQL,token,account); 
		if( row ==  0 ) {
			return getJdbcTemplate().update(AccountDaoSql.INSERT_INTO_TOKEN_FOR_CUSTOMER_SQL,token,account) > 0 ;
		}
		return true;
	}

	@Override
	public boolean userAccount(final CustomerAccountRequest request) {
		final int row = getJdbcTemplate().update(AccountDaoSql.INSERT_CUSTOME_SQL,
				request.getName(),
				request.getDiYu(),
				request.getShengShiXian(),
				request.getFZR(),
				request.getShouJi(),
				request.getYouBian(),
				request.getJiaoTongZhuangKuang(),
				request.getCanYeDingWei(),
				request.getGuLiYinJinCanYe(),
				request.getGuiHuaYongDiMianJi(),
				request.getDiJia(),
				request.getSuiJia(),
				request.getDianJia(),
				request.getNaoDongLiShuLiang(),
				request.getChengBen(),
				request.getSuiShouZhengChe(),
				request.getFuWuHuanJin(),
				request.getXianYouZhuYaoCanYe(),
				request.getPeiTaoQingKuang(),
				request.getZhiMingQiYe(),
				request.getShangHui(),
				request.getDuiJieBuMen(),
				request.getDuiJieBuMenFuZhiRen(),
				request.getDuiJieBuMenFuZhiRenShouJi(),
				request.getDuiJieBuMenFuZhiRenYouXiang(),
				request.getUserID(),
				request.getUserPAssword(),
				request.getSystemOper(),
				request.getLaiYuan());
		if ( row > 0 ) {
			return true;
		}
		return false;
	}

	@Override
	public List<CustomerBean> queryAllCustomers() {
		SqlRowSet set = getJdbcTemplate().queryForRowSet(AccountDaoSql.QUERY_ALl_CUSTOMER_SQL);
		final List<CustomerBean> cutomers = new ArrayList<CustomerBean>();
		while(set.next()){
			final CustomerBean customer = new CustomerBean();
			customer.setCanYeDingWei(set.getString("CanYeDingWei"));
			customer.setChengBen(set.getString("ChengBen"));
			customer.setDianJia(set.getString("DianJia"));
			customer.setDiJia(set.getString("DiJia"));
			customer.setDiYu(set.getString("DiYu"));
			customer.setDuiJieBuMen(set.getString("DuiJieBuMen"));
			customer.setDuiJieBuMenFuZhiRen(set.getString("DuiJieBuMenFuZhiRen"));
			customer.setDuiJieBuMenFuZhiRenShouJi(set.getString("DuiJieBuMenFuZhiRenShouJi"));
			customer.setDuiJieBuMenFuZhiRenYouXiang(set.getString("DuiJieBuMenFuZhiRenYouXiang"));
			customer.setFuWuHuanJin(set.getString("FuWuHuanJin"));
			customer.setFZR(set.getString("FZR"));
			customer.setGuiHuaYongDiMianJi(set.getString("GuiHuaYongDiMianJi"));
			customer.setGuLiYinJinCanYe(set.getString("GuLiYinJinCanYe"));
			customer.setJiaoTongZhuangKuang(set.getString("JiaoTongZhuangKuang"));
			customer.setLaiYuan(set.getString("LaiYuan"));
			customer.setName(set.getString("Name"));
			customer.setNaoDongLiShuLiang(set.getString("NaoDongLiShuLiang"));
			customer.setPeiTaoQingKuang(set.getString("PeiTaoQingKuang"));
			customer.setShangHui(set.getString("ShangHui"));
			customer.setShengShiXian(set.getString("ShengShiXian"));
			customer.setShouJi(set.getString("ShouJi"));
			customer.setSuiJia(set.getString("SuiJia"));
			customer.setSuiShouZhengChe(set.getString("SuiShouZhengChe"));
			customer.setSysID(set.getString("SysID"));
			customer.setSystemOper(set.getString("SystemOper"));
			customer.setSystemTime(set.getString("SystemTime"));
			customer.setUserID(set.getString("UserID"));
			customer.setUserPAssword(set.getString("UserPAssword"));
			customer.setXianYouZhuYaoCanYe(set.getString("XianYouZhuYaoCanYe"));
			customer.setYouBian(set.getString("YouBian"));
			customer.setZhiMingQiYe(set.getString("ZhiMingQiYe"));
			customer.setActiveTime(set.getString("ActiveTime"));
			customer.setInvalidTime(set.getString("InvalidTime"));
			customer.setSendID(set.getString("SendID"));
			cutomers.add(customer);
		}
		if( null != cutomers && cutomers.size() > 0 ) {
			return cutomers;
		}
		return null;
	}

	@Override
	public boolean isInDispaly(final String userId) {
		final String sql = " select SysID  from TBL_UserInfo where SysID = ? and ActiveTime < getdate() and getdate() < InvalidTime";
		final List<String> ids = getJdbcTemplate().queryForList(sql,String.class,userId);
		if( null != ids && ids.size() > 0 ) {
			if ( CommonUtil.isNotEmpty(ids.get(0))) {
				return false;
			}
		}
		return true;
	}
	
	public boolean creteUser(final CreteUserRequest request){
		if (getJdbcTemplate().update(AccountDaoSql.UPDATE_CUSTOMER_ALL_SQL,
				request.getName(),
				request.getDiYu(),
				request.getShengShiXian(),
				request.getFzr(),
				request.getShouJi(),
				request.getYouBian(),
				request.getJiaoTongZhuangKuang(),
				request.getCanYeDingWei(),
				request.getGuLiYinJinCanYe(),
				request.getGuiHuaYongDiMianJi(),
				request.getDiJia(),
				request.getSuiJia(),
				request.getDianJia(),
				request.getNaoDongLiShuLiang(),
				request.getChengBen(),
				request.getSuiShouZhengChe(),
				request.getFuWuHuanJin(),
				request.getXianYouZhuYaoCanYe(),
				request.getPeiTaoQingKuang(),
				request.getZhiMingQiYe(),
				request.getShangHui(),
				request.getDuiJieBuMen(),
				request.getDuiJieBuMenFuZhiRen(),
				request.getDuiJieBuMenFuZhiRenShouJi(),
				request.getDuiJieBuMenFuZhiRenYouXiang(),
				request.getUserID(),
				request.getUserPAssword(),
				request.getSystemOper(),
				request.getSystemTime(),
				request.getLaiYuan(),
				request.getActiveTime(),
				request.getInvalidTime(),
				request.getSendID(),
				request.getSysID()) > 0  ) {
			return true;
		}
		else {
			return getJdbcTemplate().update(AccountDaoSql.INSERT_CUSTOMER_ALL_SQL,
					request.getSysID(),
					request.getName(),
					request.getDiYu(),
					request.getShengShiXian(),
					request.getFzr(),
					request.getShouJi(),
					request.getYouBian(),
					request.getJiaoTongZhuangKuang(),
					request.getCanYeDingWei(),
					request.getGuLiYinJinCanYe(),
					request.getGuiHuaYongDiMianJi(),
					request.getDiJia(),
					request.getSuiJia(),
					request.getDianJia(),
					request.getNaoDongLiShuLiang(),
					request.getChengBen(),
					request.getSuiShouZhengChe(),
					request.getFuWuHuanJin(),
					request.getXianYouZhuYaoCanYe(),
					request.getPeiTaoQingKuang(),
					request.getZhiMingQiYe(),
					request.getShangHui(),
					request.getDuiJieBuMen(),
					request.getDuiJieBuMenFuZhiRen(),
					request.getDuiJieBuMenFuZhiRenShouJi(),
					request.getDuiJieBuMenFuZhiRenYouXiang(),
					request.getUserID(),
					request.getUserPAssword(),
					request.getSystemOper(),
					request.getSystemTime(),
					request.getLaiYuan(),
					request.getActiveTime(),
					request.getInvalidTime(),
					request.getSendID()) > 0 ;
		}
	}

}
